// slice 切片-数据类型[高级]
package main

import "fmt"

func main() {
	// 切片数据
	var arr1 [6]int
	var slice1 []int = arr1[2:5]

	for i := 0; i < len(arr1); i++ {
		arr1[i] = i
	}

	for i := 0; i < len(slice1); i++ {
		fmt.Printf("Slice at %d is %d\n", i, slice1[i])
	}

	slice1 = slice1[0:4]
	for i := 0; i < len(slice1); i++ {
		fmt.Printf("Slice at %d is %d\n", i, slice1[i])
	}

	// 将切片传递给函数
	fmt.Printf("Sum is: %d\n", sum(arr1[:]))

	// 使用 make 生成切片
	var slice2 []int = make([]int, 10)
	for i := 0; i < len(slice2); i++ {
		slice2[i] = 5 * i
		fmt.Printf("Slice at %d is %d\n", i, slice2[i])
	}
}

func sum(a []int) int {
	s := 0
	for i := 0; i < len(a); i++ {
		s += a[i]
	}
	return s
}